import 'package:flutter/material.dart';
import '../../../../data/models/directory.dart' as dir;

/// 文件浏览器路径导航栏组件
/// 显示当前目录路径，并提供导航功能
class PathNavigationBar extends StatelessWidget {
  /// 目录路径历史
  final List<dir.Directory> pathHistory;

  /// 导航到指定目录的回调
  final Function(int) onNavigate;

  /// 返回上级目录的回调
  final VoidCallback onNavigateToParent;

  /// 搜索按钮点击回调
  final VoidCallback onSearch;

  const PathNavigationBar({
    Key? key,
    required this.pathHistory,
    required this.onNavigate,
    required this.onNavigateToParent,
    required this.onSearch,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      padding: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: Theme.of(context).scaffoldBackgroundColor,
        border: Border(
          bottom: BorderSide(color: Theme.of(context).dividerColor, width: 1),
        ),
      ),
      child: Row(
        children: [
          // 返回按钮
          if (pathHistory.length > 1)
            IconButton(
              icon: const Icon(Icons.arrow_back),
              onPressed: onNavigateToParent,
              tooltip: '返回上级目录',
            ),

          // 目录路径
          Expanded(
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(children: _buildBreadcrumbs(context)),
            ),
          ),

          // 搜索按钮
          IconButton(
            icon: const Icon(Icons.search),
            onPressed: onSearch,
            tooltip: '搜索文件',
          ),
        ],
      ),
    );
  }

  /// 构建面包屑导航
  List<Widget> _buildBreadcrumbs(BuildContext context) {
    List<Widget> breadcrumbs = [];

    for (int i = 0; i < pathHistory.length; i++) {
      final directory = pathHistory[i];
      final isLast = i == pathHistory.length - 1;

      // 添加目录名称
      breadcrumbs.add(
        GestureDetector(
          onTap: isLast ? null : () => onNavigate(directory.id),
          child: Text(
            directory.name,
            style: TextStyle(
              color: isLast ? null : Theme.of(context).colorScheme.primary,
              fontWeight: isLast ? FontWeight.bold : null,
            ),
          ),
        ),
      );

      // 添加分隔符
      if (!isLast) {
        breadcrumbs.add(
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 8),
            child: Icon(Icons.chevron_right, size: 16),
          ),
        );
      }
    }

    return breadcrumbs;
  }
}
